package com.cinema.dao.imp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cinema.Bean.Hall;
import com.cinema.Bean.Movie;
import com.cinema.Bean.MovieSession;
import com.cinema.Bean.Seats;
import com.cinema.Bean.Ticket;
import com.cinema.dao.BaseDao;
import com.cinema.dao.ISeatsDao;
import com.mysql.jdbc.PreparedStatement;
/**
 * 座位行表的mysql数据库实现类
 **/
public class SeatsDaoMysql extends BaseDao implements ISeatsDao {

	public int insert(Seats u) {
		String sql = "insert into seats(seatId,hallId,seatRow,seatCount) values(?,?,?,?)";
		String[] args = { u.getSeatId(),u.getHallId(),u.getSeatRow()+"",u.getSeatCount()+""};
		return change(sql, args);
	}

	public int delete(Seats u) {
		String sql = "delete from seats where seatId=?";
		String[] args = { u.getSeatId()};
		return change(sql, args);
	}

	public int update(Seats u) {
		String sql = "update seats set hallId=?, seatRow=?,seatCount=? where seatId=?";
		String[] args = {u.getHallId(),u.getSeatRow()+"",u.getSeatCount()+"",u.getSeatId() };
		return change(sql, args);
	}
	/**
	 * 根据id进行查询
	 */
	public Seats SelectById(String id) {
		// TODO Auto-generated method stub
		Seats a = null;
		String sql = "select * from seats where seatId=?";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql);
			pstmt.setString(1, id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				a = new Seats();
				a.setSeatId(rs.getString("seatId"));
				a.setHallId(rs.getString("hallId"));
				a.setSeatRow(rs.getInt("seatRow"));
				a.setSeatCount(rs.getInt("seatCount"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}
	/**
	 * 查询所有座位并根据id排序
	 */
	public List<Seats> selectAll() {
		// TODO Auto-generated method stub
		List<Seats> list = new ArrayList<Seats>();
		Seats a = null;
		String sql = "select * from seats order by seatId";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Seats();
				a.setSeatId(rs.getString("seatId"));
				a.setHallId(rs.getString("hallId"));
				a.setSeatRow(rs.getInt("seatRow"));
				a.setSeatCount(rs.getInt("seatCount"));
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据关键字进行查询
	 */
	public List<Seats> Select(String keyword) {
		// TODO Auto-generated method stub
		List<Seats> list = new ArrayList<Seats>();
		Seats a = null;
		String sql = "select * from seats where hallId like '%" + keyword + "%'";
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Seats();
				a.setSeatId(rs.getString("seatId"));
				a.setHallId(rs.getString("hallId"));
				a.setSeatRow(rs.getInt("seatRow"));
				a.setSeatCount(rs.getInt("seatCount"));
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 根据场厅id进行查询
	 */
	public List<Seats> selectByHall(String where) {
		List<Seats> data=new ArrayList<Seats>();
		Seats a = null;
		String sql = "select * from seats where hallId=?"; // where字句
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			
			pstmt.setString(1, where);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				a = new Seats();
				a.setSeatId(rs.getString("seatId"));
				a.setHallId(rs.getString("hallId"));
				a.setSeatRow(rs.getInt("seatRow"));
				a.setSeatCount(rs.getInt("seatCount"));
				data.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	/**
	 * 根据传入条件进行查询
	 */
	public List<Map<String,Object>> selectByWhere(String where) {
		Map map;	
		// TODO Auto-generated method stub
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		MovieSession a = null;
		Hall h = null;
		Movie m=null;
		String sql = "select * from session s,hall h,movie m where s.hallId=h.hallId and m.movieId=s.movieId "; // where字句
		if(where!=null && !where.trim().equals("")) {
			sql+="and "+where;
		}
		Connection con = getCon();
		try {
			PreparedStatement pstmt = (PreparedStatement) con
					.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				map = new HashMap();
				a = new MovieSession();
				h = new Hall();
				m=new Movie();
				a.setSessionId(rs.getString("sessionId"));
				a.setHallId(rs.getString("hallId"));
				a.setMovieId(rs.getString("movieId"));
				a.setTime(rs.getString("time"));
				a.setPrice(rs.getDouble("price"));
				h.setHallId(rs.getString("hallId"));
				h.setHallName(rs.getString("hallName"));
				m.setMovieName(rs.getString("movieName"));
				m.setMovieId(rs.getString("movieId"));
				m.setStarring(rs.getString("starring"));
				m.setDetail(rs.getString("detail"));
				m.setDuration(rs.getInt("duration"));
				m.setDirector(rs.getString("director"));
				m.setLanguage(rs.getString("language"));
				m.setMovieType(rs.getString("movieType"));
				m.setSlogan(rs.getString("slogan"));
				m.setPoster(rs.getString("poster"));
				m.setManActorPic(rs.getString("manActorPic"));
				m.setWomanActorPic(rs.getString("womanActorPic"));
				m.setReleaseTime(rs.getString("releaseTime"));
				m.setMovieStat(rs.getInt("movieStat"));
				map.put("movieSession",a);
				map.put("hall",h);
				map.put("movie", m);
				list.add(map);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}


}
